package com.ruoyi.mall.front.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.entity.MallAddress;
import com.ruoyi.mall.entity.MallMembers;
import com.ruoyi.mall.entity.MallSign;
import com.ruoyi.mall.entity.Mask;
import com.ruoyi.mall.service.IMallAddressService;
import com.ruoyi.mall.service.IMallMembersService;
import com.ruoyi.mall.service.IMallSignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 会员签到Controller
 *
 * @author ruoyi
 * @date 2020-05-16
 */
@RestController
@RequestMapping("/mall/front/sign")
public class FrontSignController extends BaseController {

    @Autowired
    private IMallSignService mallSignService;
    @Autowired
    private IMallMembersService mallMembersService;
    @GetMapping("/{phone}")
    public AjaxResult address(@PathVariable("phone") String phone) {
        String  signMonth = DateUtils.getYyyyMm();
        List<MallSign> mallSigns = mallSignService.selectSignsByPhone(phone,signMonth);
        return AjaxResult.success(mallSigns);
    }

    /**
     * 新增保存会员签到
     */
    @Log(title = "会员签到", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody MallSign mallSign) throws ParseException {
        MallMembers mallMembers = mallMembersService.selectMallMembersById(mallSign.getMembersId());
        if(null == mallMembers){
            return AjaxResult.error("还没登录！请登录后签到");
        }

       return mallSignService.sign(mallSign,mallMembers);
    }


    /**
     * 修改会员签到
     */
    @GetMapping("/edit/{signId}")
    public AjaxResult edit(@PathVariable("signId") Long signId) {
        MallSign mallSign = mallSignService.selectMallSignById(signId);
        return AjaxResult.success(mallSign);
    }

    /**
     * 修改保存会员签到
     */
    @Log(title = "会员签到", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody MallSign mallSign) {
        return toAjax(mallSignService.updateMallSign(mallSign));
    }

    /**
     * 删除会员签到
     */
    @Log(title = "会员签到", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(mallSignService.deleteMallSignByIds(ids));
    }
}
